/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import ru.irenproject.store.AbstractThing;
import ru.irenproject.store.Event;
import ru.irenproject.store.Stash;

import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;
import com.google.protobuf.ByteString;

public final class QuestionList extends AbstractThing implements Iterable<QuestionItem> {
  public enum ChangeEvent implements Event { INSTANCE }

  @Stash private TestProtos.QuestionList.Builder $;

  @Inject QuestionList() {}

  public void addItem(QuestionItem item) {
    $.addItem(item.id());
    post(ChangeEvent.INSTANCE);
  }

  public List<ByteString> itemIdList() {
    return $.getItemList();
  }

  public ByteString itemIds(int index) {
    return $.getItem(index);
  }

  public QuestionItem items(int index) {
    return get($.getItem(index), QuestionItem.class);
  }

  @Override public UnmodifiableIterator<QuestionItem> iterator() {
    return Iterators.unmodifiableIterator(itemList().iterator());
  }

  private List<QuestionItem> itemList() {
    List<QuestionItem> res = new ArrayList<>();
    getItemList(res);
    return res;
  }

  public void getItemList(Collection<? super QuestionItem> out) {
    store().getBatchTo(itemIdList(), QuestionItem.class, out);
  }

  public boolean isEmpty() {
    return itemCount() == 0;
  }

  public int itemCount() {
    return $.getItemCount();
  }

  public Map<ByteString, Integer> getItemIdToIndexMap() {
    Map<ByteString, Integer> res = new HashMap<>();
    int n = itemCount();
    for (int i = 0; i < n; ++i) {
      res.put($.getItem(i), i);
    }
    return res;
  }

  public boolean containsItem(QuestionItem item) {
    return $.getItemList().contains(item.id());
  }

  public void removeItems(Iterable<QuestionItem> itemsToRemove) {
    LinkedHashSet<ByteString> items = new LinkedHashSet<>($.getItemList());
    if (Utils.removeAll(items, Utils.getThingIds(itemsToRemove))) {
      $.clearItem().addAllItem(items);
      post(ChangeEvent.INSTANCE);
    }
  }

  public void moveItem(int index, boolean forward) {
    checkItemIndex(index);
    int newIndex = index + (forward ? 1 : -1);
    checkItemIndex(newIndex);

    ByteString temp = $.getItem(index);
    $.setItem(index, $.getItem(newIndex));
    $.setItem(newIndex, temp);

    post(ChangeEvent.INSTANCE);
  }

  private void checkItemIndex(int index) {
    TestCheck.input(index >= 0);
    TestCheck.input(index < itemCount());
  }
}
